package dws_tb

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"
)

type DwsTbApi struct {
}

var dwsTbService = DwsTbService{}


func (dwsTbApi *DwsTbApi) CreateDwsTb(c *gin.Context) {
	var dwsTb DwsTb
	err := c.ShouldBindJSON(&dwsTb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsTb.CreatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"ApiId":     {utils.NotEmpty()},
		"SrcSchema": {utils.NotEmpty()},
		"SrcTbName": {utils.NotEmpty()},
		"Order":     {utils.NotEmpty()},
	}
	if err := utils.Verify(dwsTb, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dwsTbService.CreateDwsTb(&dwsTb); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func (dwsTbApi *DwsTbApi) DeleteDwsTb(c *gin.Context) {
	var dwsTb DwsTb
	err := c.ShouldBindJSON(&dwsTb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsTb.DeletedBy = utils.GetUserID(c)
	if err := dwsTbService.DeleteDwsTb(dwsTb); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}


func (dwsTbApi *DwsTbApi) DeleteDwsTbByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := dwsTbService.DeleteDwsTbByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}


func (dwsTbApi *DwsTbApi) UpdateDwsTb(c *gin.Context) {
	var dwsTb DwsTb
	err := c.ShouldBindJSON(&dwsTb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsTb.UpdatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"ApiId":     {utils.NotEmpty()},
		"SrcSchema": {utils.NotEmpty()},
		"SrcTbName": {utils.NotEmpty()},
		"Order":     {utils.NotEmpty()},
	}
	if err := utils.Verify(dwsTb, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dwsTbService.UpdateDwsTb(dwsTb); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func (dwsTbApi *DwsTbApi) FindDwsTb(c *gin.Context) {
	var dwsTb DwsTb
	err := c.ShouldBindQuery(&dwsTb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if redwsTb, err := dwsTbService.GetDwsTbById(dwsTb.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redwsTb": redwsTb}, c)
	}
}


func (dwsTbApi *DwsTbApi) GetDwsTbList(c *gin.Context) {
	var pageInfo DwsTbSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := dwsTbService.GetDwsTbInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func (dwsTbApi *DwsTbApi) GetAllDwsTbs(c *gin.Context) {
	dwsTb, err := dwsTbService.GetAllDwsTbs()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dwsTb": dwsTb}, "获取成功", c)
	}
}


func (dwsTbApi *DwsTbApi) GetDwsTb(c *gin.Context) {
	dwsTb, err := dwsTbService.GetDwsTb()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dwsTb": dwsTb}, "获取成功", c)
	}
}


func (dwsTbApi *DwsTbApi) InitDwsTb(c *gin.Context) {
	response.FailWithMessage("获取初始化数据失败", c)
}
